package ungdung_quanlymathang;

import java.util.ArrayList;
import java.util.List;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;

public class Cosodulieu extends SQLiteOpenHelper {
	private static final String database_name="Danhsach_muasam";
	private static final String bang_1="nguoi_dung";
	private static final String cot1_bang1="id";
	private static final String cot2_bang1="ten";
	private static final String cot3_bang1="mat_khau";
	private static final String cot4_bang1="so_dt";
	private static final String cot5_bang1="email";
	
	private static final String bang_2="mat_hang";
	private static final String cot1_bang2="id_mat_hang";
	private static final String cot2_bang2="id_nguoi_dung";
	private static final String cot3_bang2="ten_mat_hang";
	private static final String cot4_bang2="gia_mat_hang";
	private static final String cot5_bang2="nha_san_xuat";
	private static final String cot6_bang2="ngay_cap_nhat";
	private static final int version_database=2;
	public Cosodulieu(Context ct){
		super(ct,database_name,null,version_database);
	}
	@Override
	public void onCreate(SQLiteDatabase db){
		db.execSQL("create table "+ bang_1 +"("+cot1_bang1+" integer primary key autoincrement,"
	+cot2_bang1+" text not null,"+cot3_bang1+" text not null,"+cot4_bang1+" text,"
	+cot5_bang1+" text);");
		db.execSQL("create table "+ bang_2+"("+cot1_bang2+" integer primary key autoincrement,"
				+cot2_bang2+" integer not null,"+cot3_bang2+" text not null,"
				+cot4_bang2+" text not null,"+cot5_bang2+" text not null, "+cot6_bang2+" text not null," +
						"FOREIGN KEY ("+cot2_bang2+") REFERENCES "+bang_1+"("+cot1_bang1+"));") ;
						
		
	}
	@Override
	public void onUpgrade(SQLiteDatabase db, int old_tb, int new_tb){
		db.execSQL("DROP TABLE IF EXISTS " + bang_1);
		db.execSQL("DROP TABLE IF EXISTS " + bang_2);
		onCreate(db);
	}
	public void them_nguoidung(nguoi_dung nd){
		SQLiteDatabase db=this.getWritableDatabase();
		ContentValues ct=new ContentValues();
		ct.put(cot2_bang1, nd.get_ten());
		ct.put(cot3_bang1, nd.get_matkhau());
		ct.put(cot4_bang1, nd.get_sodt());
		ct.put(cot5_bang1, nd.get_email());
		db.insert(bang_1, null,ct);
		db.close();
		
	}
	public void them_mathang(mat_hang mh){
		SQLiteDatabase db=this.getWritableDatabase();
		ContentValues ct=new ContentValues();
		ct.put(cot2_bang2, mh.get_id_nguoidung());
		ct.put(cot3_bang2, mh.get_ten_mathang());
		ct.put(cot4_bang2, mh.get_gia_mathang());
		ct.put(cot5_bang2, mh.get_nha_sanxuat());
		ct.put(cot6_bang2, mh.get_ngay_capnhat());
		db.insert(bang_2, null,ct);
		db.close();
	}
	
	public int capnhat_nguoidung(nguoi_dung nd){
		SQLiteDatabase db=this.getWritableDatabase();
		ContentValues ct=new ContentValues();
		ct.put(cot2_bang1, nd.get_ten());
		ct.put(cot3_bang1, nd.get_matkhau());
		ct.put(cot4_bang1, nd.get_sodt());
		ct.put(cot5_bang1, nd.get_email());
		return db.update(bang_1,ct,cot1_bang1+"=?",new String[] {String.valueOf(nd.get_id())});
	
	}
	public int capnhat_mathang(mat_hang mh){
		SQLiteDatabase db=this.getWritableDatabase();
		ContentValues ct=new ContentValues();
		ct.put(cot2_bang2, mh.get_id_nguoidung());
		ct.put(cot3_bang2, mh.get_ten_mathang());
		ct.put(cot4_bang2, mh.get_gia_mathang());
		ct.put(cot5_bang2, mh.get_nha_sanxuat());
		ct.put(cot6_bang2, mh.get_ngay_capnhat());
		return db.update(bang_2,ct,cot1_bang2+"=?",new String[] {String.valueOf(mh.get_id_mathang())});
		
	}
	
	public void xoa_nguoidung(nguoi_dung nd){
		SQLiteDatabase db=this.getWritableDatabase();
		db.delete(bang_1,cot1_bang1+"=?",new String[]{String.valueOf(nd.get_id())});
		db.close();
	}
	public void xoa_mathang(mat_hang mh){
		SQLiteDatabase db=this.getWritableDatabase();
		db.delete(bang_2,cot1_bang2+"=?",new String[]{String.valueOf(mh.get_id_mathang())});
		db.close();
	}
	
	public Cursor lay_nguoidung(String pwd)
	  {
	   SQLiteDatabase db=this.getReadableDatabase();
	   String [] columns=new String[]{cot1_bang1,cot2_bang1};
	   Cursor c=db.query(bang_1, columns, cot3_bang1+"=?", 
		new String[]{pwd}, null, null, null);
	   return c;
	  }
	public Cursor lay_idnguoidung(String ten,String matkhau){
		SQLiteDatabase db=this.getReadableDatabase();
		String [] columns=new String[]{cot1_bang1};
		Cursor c=db.query(bang_1, columns, cot2_bang1+"=? "+"and "+ cot3_bang1+"=?" , 
		new String[]{ten,matkhau},null, null,null);
		return c;
	}
	public Cursor lay_mathang(String id){
		SQLiteDatabase db=this.getReadableDatabase();
		String [] columns=new String[]{cot3_bang2};
		Cursor c=db.query(bang_2, columns, cot2_bang2+"=? ", 
		new String[] {id}, null, null, null);
		return c;
	}	
	public List<mat_hang> getall_mathang(int id) {
		
	    List<mat_hang> list_mathang = new ArrayList<mat_hang>();
	    String [] columns=new String[]{cot1_bang2,cot2_bang2,cot3_bang2,cot4_bang2,cot5_bang2,cot6_bang2};
	    SQLiteDatabase db = this.getWritableDatabase();
	 	Cursor c=db.query(bang_2, columns, cot2_bang2+"=? ", 
	 			new String[] {String.valueOf(id)}, null, null, null);
	 	   	    if (c.moveToFirst()) {
	        do {
	            mat_hang mh = new mat_hang();
	            mh.set_id_mathang(c.getInt(0));
	            mh.set_id_nguoidung(c.getInt(1));
	            mh.set_tenmathang(c.getString(2));
	            mh.set_gia_mathang(c.getString(3));
	            mh.set_nha_sanxuat(c.getString(4));
	            mh.set_ngay_capnhat(c.getString(5));
	            list_mathang.add(mh);
	        } while (c.moveToNext());
	    }
	 
	    	    return list_mathang;
		
	}
}

